package com.hz.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.hz.web.entity.CkHtmlEntity;
import com.hz.web.entity.GhEnterpriseTableEntity;
import com.hz.web.entity.Objectentity;
import com.hz.web.service.IckHtmlService;
import com.hz.web.service.ImanyDemoService;
import com.hz.web.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("app")
public class AppController {

    @Resource
    private IckHtmlService service;

    @Resource
    private ImanyDemoService manyDemoService;


    /**
     * 查询信用动态
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("queryCreditDynamic")
    @ResponseBody
    public Map<String, Object> queryCkHtml(int page,int ckType,@RequestParam(value = "rows", required = false) Integer rows) throws UnsupportedEncodingException {
        //富文本编辑器  区分每个功能的字段
        // 信用动态：ckType = 6;
        // 政策法规：ckType = 22;
        //信用黑榜： ckType=31;
        //信用红榜：ckType=30;

        String ckTypeList = null;//富文本编辑器  区分每个功能的字段
        if (null == rows || 0 ==rows) {
            rows = 10;
        }
        String userName = null;
        String showName = null;
        String state = "1";
        ParmsEntity entity = new ParmsEntity();
        entity.setCkType(ckType);
        entity.setCkTypeList(ckTypeList);
        entity.setPage(page);
        entity.setRows(rows);
        entity.setState(state);

        String sort = "yes";

        return queryCkHtml(entity, sort, null, null);
    }

    public Map<String, Object> queryCkHtml(ParmsEntity entity, String sort, String inputVal,String listCktype) throws UnsupportedEncodingException {
        if (inputVal != null && !"".equals(inputVal.trim())) {
            // inputVal = URLDecoder.decode(URLDecoder.decode(inputVal));
            inputVal = URLDecoder.decode(URLDecoder.decode(inputVal, "utf-8"), "utf-8");
        }
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        int ckType = entity.getCkType();
        int page = entity.getPage();
        int rows = entity.getRows();
        String state = entity.getState();
        /*if(ckType==0){
        	return result;
        }*/
        if (inputVal != null && !"".equals(inputVal.trim())) {
            parmsMap.put("inputVal", "%" + inputVal.trim() + "%");
        }
        if (state != null && !"".equals(state.trim())) {
            parmsMap.put("state", state.trim());
        }
        if (listCktype != null && !"".equals(listCktype.trim())) {
        	List<String> list = new ArrayList<String>();
        	String[] listSz =listCktype.split(",");
        	for (String string : listSz) {
        		list.add(string);
			}
            parmsMap.put("list", list);
        }
        
        parmsMap.put("ckType", ckType);// 富文本类型 判断是哪个功能的富文本信息
        parmsMap.put("sort", sort);
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("end", end);
            int count = 0;
            count = service.queryCkHtmlCount(parmsMap);
            result.put("total", count);
        }

        // 根据参数map查询出的富文本编辑的结果集合
        List<CkHtmlEntity> list = new ArrayList<CkHtmlEntity>();
        list = service.queryCkHtml(parmsMap);
        String htmlName = "";
        htmlName = UtilMethods.getNameByHtmlType(entity.getCkType());
        for (CkHtmlEntity ckHtml : list) {
            ckHtml.setTypeName(htmlName);
        }
        result.put("htmlName", htmlName);
        result.put("rows", list);
        result.put("resultCode", "0");
        result.put("resultMsg", "查询成功");
        return result;
    }


    /**
     * 查询法人信息
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "queryBzxy", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryBzxy(String qymc)throws UnsupportedEncodingException  {

        qymc = URLEncoder.encode(URLEncoder.encode(qymc, "UTF-8"),"UTF-8");//编码
        String url = "/web/SgsInterfaceAction.do?dispatch=getFrjbxx&page=1&rows=10";
        return queryWfxy(url, null, null, null, null, null, null, null, qymc, null);
    }

    public JSONObject queryWfxy(String url, String xm, String sfzhm, String zzjgdm,
                                String gszch, String yt, String frdbxm, String jqcz, String qymc,
                                String tyshxydm) {
        String utilIp = ConstantUtils.getInterfaceUrl("urlStr");
        String resultJsonStr = "";
        url = url+"&qymc="+qymc+"&zzjgdm="+zzjgdm+"&gszch="+gszch;
        String allUrl = utilIp+url;
        try {
            resultJsonStr = HttpUtils.httpClient(new JSONObject(), allUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject json = JSONObject.parseObject(resultJsonStr);
        json.put("utilip", utilIp);
        json.put("resultCode", "0");
        json.put("resultMsg", "查询成功");
        return json;
    }

    /**
     * 查询企业基本信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "queryQyMsgByEnterpriseId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryQyMsgByEnterpriseId(String enterpriseid) {
        if (enterpriseid == null || "".equals(enterpriseid)) {
            return null;
        }
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        parmsMap.put("enterpriseId", enterpriseid);
        List<GhEnterpriseTableEntity> list = manyDemoService.queryQyMsgByEnterpriseId(parmsMap);
        List newList = new ArrayList<>();

        for (GhEnterpriseTableEntity ghEnterpriseTableEntity : list) {
            newList.add(new Objectentity("法人Id", ghEnterpriseTableEntity.getENTERPRISE_ID()));
            newList.add(new Objectentity("企业名称", ghEnterpriseTableEntity.getV_QY_INDEX_2060()));
            newList.add(new Objectentity("统一社会信用代码", ghEnterpriseTableEntity.getV_QY_INDEX_2061()));
            newList.add(new Objectentity("类别", ghEnterpriseTableEntity.getV_QY_INDEX_2062()));
            newList.add(new Objectentity("企业（机构）类型", ghEnterpriseTableEntity.getV_QY_INDEX_2063()));
            newList.add(new Objectentity("注册资本（万元）", ghEnterpriseTableEntity.getV_QY_INDEX_2064()));
            newList.add(new Objectentity("注册资本（金）币种", ghEnterpriseTableEntity.getV_QY_INDEX_2065()));
            newList.add(new Objectentity("行业门类", ghEnterpriseTableEntity.getV_QY_INDEX_2066()));
            newList.add(new Objectentity("成立日期", ghEnterpriseTableEntity.getV_QY_INDEX_2067()));
            newList.add(new Objectentity("登记机关", ghEnterpriseTableEntity.getV_QY_INDEX_2068()));
            newList.add(new Objectentity("地址", ghEnterpriseTableEntity.getV_QY_INDEX_2069()));
            newList.add(new Objectentity("状态", ghEnterpriseTableEntity.getV_QY_INDEX_2070()));
            newList.add(new Objectentity("经营范围", ghEnterpriseTableEntity.getV_QY_INDEX_2071()));
        }

        result.put("rows", list);
        result.put("resultCode", "0");
        result.put("resultMsg", "查询成功");
        return result;
    }



}
